/*******************************************************************************
 * Copyright (c) 2013 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
/**
 * 
 */
package com.niannian.activity;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.niannian.BaseActivity;
import com.niannian.R;
import com.niannian.dialog.YesNoDialog2;
import com.niannian.dialog.YesNoDialog2.onSimpleOnCancelListener;
import com.niannian.dialog.YesNoDialog2.onSimpleOnClicklListener;
import com.niannian.util.SpocketHelper;
import com.niannian.util.UmengPage;
import com.niannian.util.Version;
import com.umeng.analytics.MobclickAgent;
import com.umeng.fb.FeedbackAgent;

/**
 * 
 * @author shengl
 * 
 */
public class SettingActivity extends BaseActivity {
	private TextView tv_about, tv_change_password, tv_updata_state, tv_feedback, tv_news_tip, tv_binding_phone;
	private Button btn_quit;
	private ImageView iv_updata_state;
	private RelativeLayout rl_update;
	private Context mContext;
	FeedbackAgent fb;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_setting);
		initView();
		setLister();
		initTopView();
		mContext = this;
		fb = new FeedbackAgent(this);
		// check if the app developer has replied to the feedback or not.
		fb.sync();
	}

	// 头部视图
	private ImageView iv_top_left, iv_top_right;
	private TextView top_title;
	private LinearLayout iv_top_left2, top_title2;

	private void initTopView() {
		iv_top_left = (ImageView) findViewById(R.id.iv_top_left);
		iv_top_left2 = (LinearLayout) findViewById(R.id.iv_top_left2);
		iv_top_left.setVisibility(View.VISIBLE);
		iv_top_left2.setVisibility(View.GONE);

		top_title = (TextView) findViewById(R.id.top_title);
		top_title2 = (LinearLayout) findViewById(R.id.top_title2);
		top_title.setVisibility(View.VISIBLE);
		top_title2.setVisibility(View.GONE);
		top_title.setText("设置");

		iv_top_right = (ImageView) findViewById(R.id.iv_top_right);
		iv_top_right.setVisibility(View.GONE);

		iv_top_left.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				myfinish();
			}
		});
	}

	private void initView() {
		tv_about = (TextView) findViewById(R.id.tv_about);
		tv_change_password = (TextView) findViewById(R.id.tv_change_password);
		rl_update = (RelativeLayout) findViewById(R.id.rl_update);
		btn_quit = (Button) findViewById(R.id.btn_quit);
		tv_updata_state = (TextView) findViewById(R.id.tv_updata_state);
		iv_updata_state = (ImageView) findViewById(R.id.iv_updata_state);
		iv_updata_state.setVisibility(View.GONE);
		tv_updata_state.setVisibility(View.VISIBLE);
		tv_updata_state.setText("v" + SpocketHelper.getPackageVersion(SettingActivity.this));

		tv_feedback = (TextView) findViewById(R.id.tv_feedback);
		tv_news_tip = (TextView) findViewById(R.id.tv_news_tip);
		tv_binding_phone = (TextView) findViewById(R.id.tv_binding_phone);
	}

	private void setLister() {

		tv_about.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				openActivity(AboutActivity.class);
				startAnimationLeftToRight();
			}
		});
		tv_news_tip.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				openActivity(NewsSoundSettingActivity.class);
				startAnimationLeftToRight();
			}
		});
		tv_feedback.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				fb.startFeedbackActivity();
				startAnimationLeftToRight();
			}
		});
		tv_change_password.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				openActivity(ChangePasswordActivity.class);
				startAnimationLeftToRight();
			}
		});
		rl_update.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// tv_updata_state.setText("正在检查...");
				iv_updata_state.setVisibility(View.GONE);
				Version version = new Version(mContext, true, true) {

					@Override
					public void showLatestVersion(String latestVersion, String desc, String downloadurl, String datetime) {

					}

					@Override
					public void showVersionState(int versionState) {
						switch (versionState) {
						case 2:
							// tv_updata_state.setText("已是最新版本");
							tv_updata_state.setText("v" + SpocketHelper.getPackageVersion(SettingActivity.this));
							iv_updata_state.setImageResource(R.drawable.updata_state_y);
							tv_updata_state.setVisibility(View.VISIBLE);
							iv_updata_state.setVisibility(View.GONE);
							break;

						case 3:
							tv_updata_state.setText("正在下载最新版本");
							tv_updata_state.setVisibility(View.VISIBLE);
							iv_updata_state.setVisibility(View.GONE);

							break;
						case 4:
							tv_updata_state.setText("有新版本");
							iv_updata_state.setImageResource(R.drawable.updata_state_n);
							tv_updata_state.setVisibility(View.VISIBLE);
							iv_updata_state.setVisibility(View.VISIBLE);

							break;
						default:
							break;
						}

					}
				};
				// version.init(null).execute();
				version.init(mContext, null, true, "正在检查...").execute();
			}
		});
		btn_quit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				initDialog();
			}
		});

		tv_binding_phone.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				openActivity(BindingPhoneActivity.class);
				startAnimationLeftToRight();
			}
		});

	}

	YesNoDialog2 tip;

	private void initDialog() {
		tip = new YesNoDialog2(SettingActivity.this, "确认要退出登录吗?");
		tip.setOnCancelListener(new onSimpleOnCancelListener() {

			@Override
			public void onCancel() {
				tip.dismiss();
			}
		});
		tip.setOnConfimClickListener(new onSimpleOnClicklListener() {

			@Override
			public void onClick() {
				userInfoManager.id = 0;
				try {
					userInfoManager.clearPref();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				destroyGroup("All");
				openActivity(LoginBeforeActivity.class);
				myfinish();
			}
		});
		tip.show();
	}

	@Override
	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart(UmengPage.SETUP);
		MobclickAgent.onResume(this);
	}

	@Override
	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd(UmengPage.SETUP);
		MobclickAgent.onPause(this);
	}
}
